package anji.plus.system.mark.controller;


import anji.plus.system.mark.base.BaseController;
import anji.plus.system.mark.base.Header;
import anji.plus.system.mark.base.IBaseService;
import anji.plus.system.mark.domain.SysUser;
import anji.plus.system.mark.response.ResponseCode;
import anji.plus.system.mark.response.ResponseResult;
import anji.plus.system.mark.service.ISysUserService;
import anji.plus.system.mark.service.impl.FlagServiceImpl;
import anji.plus.system.mark.utils.LoginUtils;
import anji.plus.system.mark.utils.PassportUtil;
import anji.plus.system.mark.utils.RedisUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author luwei
 * @since 2023-11-08
 */
@RestController
@RequestMapping("sys/user")
public class SysUserController extends BaseController {


    @Resource
    private ISysUserService sysUserService;

    @Resource
    RedisUtil redisUtil;

    @Resource
    LoginUtils loginUtils;

    @Override
    public IBaseService getService() {
        return null;
    }

    private static Log log = LogFactory.get(SysUserController.class);
    /**
     * 方法实现说明 根据用户查询公司树
     *
     * @return
     * @throws
     * @author terry
     * @date 2022/5/31 11:47
     */
    @GetMapping({"queryUserList"})
    public ResponseResult queryUserList(String departNum) {
        return ResponseResult.success(sysUserService.queryUserList(departNum));
    }



    @PostMapping({"login"})
    public ResponseResult login(String account, String password) {
        if ((account == null) && (password == null)) {
            // 判断账号，密码都为空
            return ResponseResult.failure(ResponseCode.PARAM_IS_BLANK);
        } else if (account == null) {
            // 判断账号为空
            return ResponseResult.failure("账户不能为空");
        } else if (password == null) {
            // 判断密码为空
            return ResponseResult.failure("密码不能为空");
        }
        // 判断验证账号是否存在，并验证密码是否正确，不匹配提示账号或密码错误；
        SysUser sysUser = sysUserService.login(account, password);
        if (sysUser != null && sysUser.getId() != null) {
            // 生成token字符串
            String token = PassportUtil.azdgcryptEncode2(account);
            // 获取accessToken
            sysUser.setToken(token);
            //setex 命令
            //setex 命令在 Redis 中用于设置一个键值对，并指定键的过期时间。其语法如下:
            //SET key value EX seconds
            //·key:键名
            //。value:键值
            //EXseconds:过期时间，以秒为单位
            //setex 命令会将键值对存储在 Redis 中，并在指定时间后自动删除该键。
            redisUtil.setex("loginInfo:" + token, sysUser, 172800);
            log.info(account+" 登录成功，登陆时间："+ DateUtil.now());
            return ResponseResult.success(sysUser);
        } else {
            return ResponseResult.failure("账户或密码不正确");
        }

    }

    @PostMapping("quit")
    public ResponseResult quit() {
        String accessToken = Header.getAuthorization(request.getHeader("Authorization"));
        SysUser sysUserDto = loginUtils.get(accessToken);
        if (sysUserDto != null) {
            redisUtil.del("loginInfo:" + accessToken);
            return ResponseResult.success();
        } else {
            return ResponseResult.failure(ResponseCode.USER_NOT_LOGGED_IN);
        }
    }

    @PostMapping({"update"})
    public ResponseResult userUpdate(String account, String name, String userId) {
        //获取当前登录用户方法
//        String token = Header.getAuthorization(request.getHeader("Authorization"));
//        SysUser sysUser = loginUtils.get(token);
//        System.out.println("********************");
//        System.out.println(sysUser.getFullname());
//        System.out.println("********************");

        if ((account == null) && (name == null)) {
            // 判断账号，密码都为空
            return ResponseResult.failure(ResponseCode.PARAM_IS_BLANK);
        }else if (account == null) {
            // 判断账号为空
            return ResponseResult.failure("账户不能为空");
        } else if (name == null) {
            // 判断密码为空
            return ResponseResult.failure("姓名不能为空");
        }else if ((1==1)){
            SysUser sysUser = sysUserService.userUpdate(account,name,userId);
            return ResponseResult.success("提交成功");
        }else{
            return ResponseResult.failure("提交失败，请联系管理员");
        }
    }


//    //获取当前登录用户方法
//    String token = Header.getAuthorization(request.getHeader("Authorization"));
//    SysUser sysUser = loginUtils.get(token);
//        System.out.println("********************");
//        System.out.println(sysUser.getFullname());
//        System.out.println("********************");


}


